package jp.co.nes.awssupport.compile.function;

import java.time.YearMonth;

import jp.co.nes.awssupport.access.dao.MonthlyCompiledBillingDao;
import jp.co.nes.awssupport.access.dao.MonthlyCompiledBillingDao.DaoReadTypeOfTotalization;
import jp.co.nes.awssupport.access.dao.PremiumSupportAssignedDao;
import jp.co.nes.awssupport.business.base.AbstractRequestWithCompletionStateHandler;
import jp.co.nes.awssupport.business.bean.CommonContainerBean.ProcessingResult;
import jp.co.nes.awssupport.business.bean.CompileContainerBean;
import jp.co.nes.awssupport.business.bean.CompileInfoBean;
import jp.co.nes.awssupport.business.logic.PremiumSupportProcessing;
import jp.co.nes.awssupport.common.bean.MonthlyCompiledBillingBean;
import jp.co.nes.awssupport.common.bean.PremiumSupportAssignedBean;
import jp.co.nes.awssupport.common.dao.ReadHandler;
import jp.co.nes.awssupport.common.dao.ReadType;
import jp.co.nes.awssupport.common.utility.BeanUtil;
import jp.co.nes.awssupport.common.utility.DateUtil;

/**
 * プレミアムサポート適用です(Lambda)。
 */
public class PremiumSupport
        extends AbstractRequestWithCompletionStateHandler<CompileInfoBean, CompileContainerBean<CompileInfoBean>> {

    /**
     * デフォルトコンストラクタです。
     */
    public PremiumSupport() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.base.AbstractRequestHandler#handleRequest(java.lang.Object)
     */
    @Override
    protected CompileContainerBean<CompileInfoBean> handleRequest(CompileInfoBean input) {
        YearMonth targetYearMonth = DateUtil.toYearMonth(input.getTargetYearMonthValue());
        String linkedAccountId = input.getLinkedAccountId();

        // PremiumSupportProcessingの生成はread()メソッドの外で実行する(契約情報の取得を複数回実行しないため)
        PremiumSupportProcessing psProcessing = new PremiumSupportProcessing(targetYearMonth);

        // 検索条件の作成
        Object[] conditions = new Object[2];
        conditions[0] = linkedAccountId;
        conditions[1] = DateUtil.toInt(targetYearMonth);

        Class<MonthlyCompiledBillingBean> cls = MonthlyCompiledBillingBean.class;
        MonthlyCompiledBillingDao dao = new MonthlyCompiledBillingDao();
        ReadType readType = DaoReadTypeOfTotalization.TOTALIZATION_FF;
        dao.read(new ReadHandler<MonthlyCompiledBillingBean>(cls, readType) {
            /*
             * (非 Javadoc)
             * @see jp.co.nes.awssupport.common.dao.ReadHandler#read(jp.co.nes.awssupport.common.bean.BaseBean)
             */
            @Override
            protected void read(MonthlyCompiledBillingBean mcbBean) {
                // 契約情報のサポートプランに関係なく、プレミアムサポート適用を削除し、
                // サポートプランがプレミアムである場合のみ、プレミアムサポート適用を登録する。
                PremiumSupportAssignedDao assignedDao = new PremiumSupportAssignedDao();
                assignedDao.delete(mcbBean.getLinkedAccountId(), targetYearMonth);

                PremiumSupportAssignedBean applyBean = psProcessing.apply(mcbBean);
                if (applyBean != null) {
                    assignedDao.insert(applyBean);
                }
            }
        }, conditions);

        // 返却内容の設定
        CompileInfoBean info = new CompileInfoBean();
        BeanUtil.copyProperties(info, input);
        CompileContainerBean<CompileInfoBean> output = new CompileContainerBean<>();
        output.setInfoBean(info);
        output.setProcessingResult(ProcessingResult.SUCCESS);
        return output;
    }
}
